﻿using System;
using System.Windows.Forms;
using DimensionsHelper.ExcelAddIn.Core.Sql;
using DimensionsHelper.ExcelAddIn.Ms.Operator;
using Microsoft.Office.Tools.Ribbon;

namespace DimensionsHelper.ExcelAddIn.Ms
{
    public partial class MainRibbon
    {

        private void MainRibbon_Load(object sender, RibbonUIEventArgs e)
        {
        }


        private void BuildSqlSettingsButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                Globals.ThisAddIn.Application.DisplayAlerts = false;
                var builder = ThisAddIn.GetService<ScriptBuilder>();
                var workbook = ExcelWorkbookOperator.FromApplication(
                    Globals.ThisAddIn.Application,
                    Globals.ThisAddIn.Application.ActiveWorkbook);
                builder.BuildSettingSheet(workbook);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Globals.ThisAddIn.Application.DisplayAlerts = true;
            }
        }


        private void BuildSqlScriptButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                Globals.ThisAddIn.Application.DisplayAlerts = false;
                var builder = ThisAddIn.GetService<ScriptBuilder>();
                var workbook = ExcelWorkbookOperator.FromApplication(
                    Globals.ThisAddIn.Application,
                    Globals.ThisAddIn.Application.ActiveWorkbook);
                builder.Run(workbook);
            }
#if !DEBUG
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
#endif
            finally
            {
                Globals.ThisAddIn.Application.DisplayAlerts = true;
                MessageBox.Show("已完成");
            }
        }


        private void BuildCodeMapSettingSheetButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                Globals.ThisAddIn.Application.DisplayAlerts = false;
                var builder = new CodeMapBuilder();
                builder.BuildSettingSheet(ExcelWorkbookOperator.FromApplication(
                    Globals.ThisAddIn.Application,
                    Globals.ThisAddIn.Application.ActiveWorkbook));
            }
#if !DEBUG
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
#endif
            finally
            {
                Globals.ThisAddIn.Application.DisplayAlerts = true;
            }
        }


        private void SettingsButton_Click(object sender, RibbonControlEventArgs e)
        {
            Globals.ThisAddIn.ShowSettingWindow();
        }
    }
}
